package com.ml.mall.api.partner;

import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.partner.*;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.service.partner.SysPartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 *   @description    :description
 *   @author         :zxm
 *   @date           :2021/5/19
 *   @version        :1.0
**/
@Api(tags = "【auth】合作商相关接口")
@RestController
@RequestMapping("/dim/partner")
@Slf4j
public class PartnerController {

    @Autowired
    private SysPartnerService sysPartnerService;

    @ApiOperation(value = "合作商邀请商户")
    @PostMapping("/inviter_supplier")
//  @RequiresPermissions(PermissionConst.DIM_PARTNER_INVITER_SUPPLIER)
    public ApiRestResult partnerInviterMerchant(@RequestBody @Valid PartnerDto dto){
        sysPartnerService.partnerInviterMerchant(dto);
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "合作商被邀请")
    @PostMapping("/inviter_partener")
//  @RequiresPermissions(PermissionConst.DIM_PARTNER_INVITER_PARTENER)
    public ApiRestResult partnerInviter(@RequestBody @Valid PartnerDto dto){
        //需要增加给合作商发送邀请邮件
        sysPartnerService.partnerInviter(dto);
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "合作商新增")
    @PostMapping("/new")
//  @RequiresPermissions(PermissionConst.DIM_PARTNER_NEW) //未发现调用
    public ApiRestResult partnerNew(@RequestBody @Valid PartnerNewDto dto){
        sysPartnerService.createPartner(dto);
        return ApiRestResult.ok();
    }

    @ApiOperation(value = "合作商查询单个")
    @PostMapping("/query_one")
//  @RequiresPermissions(PermissionConst.DIM_PARTNER_QUERY_ONE)
    public ApiRestResult partnerQueryOne(@RequestBody @Valid PartnerQueryOneDto dto){
        return ApiRestResult.ok(sysPartnerService.queryOnePartner(dto));
    }

    @ApiOperation(value = "合作商分页查询")
    @PostMapping("/query_page")
//  @RequiresPermissions(PermissionConst.DIM_PARTNER_QUERY_PAGE)
    public ApiRestResult partnerQueryPage(@RequestBody PartnerQueryDto dto){
        return ApiRestResult.ok(sysPartnerService.queryPagePartner(dto));
    }

    @ApiOperation(value = "取消关联")
    @PostMapping("/del_reletion")
//  @RequiresPermissions(PermissionConst.DIM_PARTNER_DEL_RELETION)
    public ApiRestResult delReletion(@RequestBody PartnerDelCompanyDto dto){
        log.info("delete relation between supplier and partner=request====={}", dto);
        //取消注册码即取消关联
        int result = sysPartnerService.updateCompanyRegister(dto);
        log.info("delete relation between supplier and partner=response====={}", result);
        return ApiRestResult.ok(result);
    }

    @ApiOperation(value = "给合伙人分配卖家")
    @PostMapping("/allocation_supplier")
//  @RequiresPermissions(PermissionConst.DIM_PARTNER_ALLOCATION_SUPPLIER)
    public ApiRestResult allocationSupplier(@RequestBody @Valid AllocationSupplierDto dto){
        return ApiRestResult.ok(sysPartnerService.allocationSupplier(dto));
    }
}
